<template>
  <el-dialog
    :title="{ add: '新增部门', edit: '编辑部门' }[mode]"
    width="600px"
    :visible.sync="show"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item prop="name" label="部门名称">
        <el-input v-model.trim="form.name" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item prop="code" label="部门编码">
        <el-input v-model.trim="form.code" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item prop="manager" label="部门负责人">
        <el-select v-model="form.manager" @focus="getUserList">
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="introduce" label="部门介绍">
        <el-input
          v-model="form.introduce"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {
  sysUserSimple,
  companyDepartmentPost,
  companyDepartmentPut
} from '@/api/departments'
export default {
  props: {
    initList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      mode: 'add', // add新增 edit编辑
      userList: [],
      show: false,
      test: '1',
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'change' },
          { min: 1, max: 50, message: '请输入1-50个字符', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              let newArr = []
              if (this.mode === 'add') {
                newArr = this.initList.filter((item) => {
                  return item.pid === this.form.pid
                })
              } else {
                // 编辑部门时，排除自己
                const arr = this.initList.filter((item) => {
                  return item.id !== this.form.id
                })
                newArr = arr.filter((item) => {
                  return item.pid === this.form.pid
                })
              }
              const bol = newArr.every((item) => {
                return item.name !== value
              })
              if (bol) {
                callback()
              } else {
                callback(new Error('重复的部门名称:' + value))
              }
            },
            trigger: 'change'
          }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'change' },
          { min: 1, max: 50, message: '请输入1-50个字符', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              let bol
              if (this.mode === 'add') {
                bol = this.initList.every((item) => {
                  return item.code !== value
                })
              } else {
                // 编辑部门时，排除自己
                const newArr = this.initList.filter((item) => {
                  return item.id !== this.form.id
                })
                bol = newArr.every((item) => {
                  return item.code !== value
                })
              }
              if (bol) {
                callback()
              } else {
                callback(new Error('重复的code:' + value))
              }
            },
            trigger: 'change'
          }
        ],
        manager: [
          { required: true, message: '请选择部门负责人', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'change' },
          { min: 1, max: 300, message: '请输入1-300个字符', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 获取员工列表，用于显示部门负责人选择一项
    async getUserList() {
      if (this.userList.length === 0) {
        const res = await sysUserSimple()
        this.userList = res.data
      }
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          if (this.mode === 'add') {
            await companyDepartmentPost(this.form)
            this.$message.success('新增部门成功')
          } else {
            await companyDepartmentPut(this.form)
            this.$message.success('修改部门成功')
          }
          this.show = false
          // 刷新列表
          this.$emit('getData')
          // 重置表单
        }
      })
    },
    cancel() {
      this.show = false
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      }
    },
    closeEvent() {
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      }
    }
  }
}
</script>

<style></style>
